#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cbm.h>
#include <conio.h>

#include "Buffers.h"
#include "Trim.h"
#include "Splitter.h"
#include "Dispatcher.h"
#include "Drives.h"

#pragma code-name("SHOWCODE");
#pragma rodata-name("SHOWCODE");

#define BUFFERSIZE (sizeof buffer2)

unsigned char showFile(unsigned char driveIndex)
{
	int r = 0, i = 0;
	unsigned int count = 0,track = 0, sector = 0;
	drive_t *drive = &drives[driveIndex];
	static char buffer[256];
	tokenCollection_t *collection = NULL;
	parsedPath_t *path = NULL;
	char file[2 + 16 + 4];
	char line[81];
	char word[81];
	char last, character = '\0';
	bool printLine = false;
	unsigned char
		counter = 0,
		currentLine = 1;

	if(0) cgetc();

	strcpy(buffer, buffer3);

	collection = splitString(buffer);

	if(collection->count != 2)
	{
		printf("\nusage: show :file\n");
		free(collection);
		return 0;
	}

	path = parsePath(1, collection, driveIndex);

	if(isPattern(path->file))
	{
		printf("\nFile cannot be a pattern.");
		free(collection);
		free(path);
		return 1;
	}

	driveIndex = getDrive(path->device);
	if(driveIndex == 0xFF) 
	{
		printf("\nUnknown device.");
		free(collection);
		free(path);
		return 1;
	}
	drive = &drives[driveIndex];

	memset(arguments, 0, sizeof(arguments));

	if(drive->type != CBM15x1)
	{
		if(drive->type == VICE)
		{
			sprintf(buffer3, "cd:%s", path->path);
		}
		else
		{
			sprintf(buffer3, "cd%s", path->path);
		}

		if(!executeCommand(drive, buffer3, 0))
		{
			printf("\nCould not change directory.");
			free(collection);
			free(path);
			return 1;
		}
	}

	if(drive->type == CBM15x1)
	{
		sprintf(file, "%d:%s,,r", drive->drive, path->file);
	}
	else
	{
		sprintf(file, ":%s,,r", path->file);
	}

	printf("\n");

	if((r = cbm_open(2, drive->device, 2, file)) == 0)
	{
		memset(word, line[0] = 0, sizeof(word));

		do
		{
			r = cbm_read(2, buffer2, BUFFERSIZE);

			for(i = 0; i < r; ++i)
			{
				last = character;
				character = buffer2[i];
				if(character == '\n' ||
					character == '\r')
				{
					if(
						(character == '\n' && last != '\r') ||
						(character == '\r' && last != '\n'))
					{
						printLine = true;

						strcat(line, word);
						memset(word, counter = 0, sizeof word);
					}
				}
				else
				{
					if ((word[counter] = character) == ' ')
					{
						strcat(line, word);
						memset(word, counter = 0, sizeof word);
					}
					else if (strlen(line) + (++counter) >= 39)
					{
						printLine = true;

						if (line[0] == '\0')
						{
							strcat(line, word);
							memset(word, counter = 0, sizeof word);
						}
					}
				}

				if(printLine)
				{
					printf("\n%s", line);
					printLine = false;
					line[0] = '\0';

					if(++currentLine == 25)
					{
						
						printf("\n%cRETURN to cont., STOP to end%c", 0x12, 0x92);
						
						if(cgetc() == CH_STOP)
						{
							cbm_close(2); cbm_close(15);
							free(collection);
							free(path);
							return 0;
						}
						else
						{
							currentLine = 1;
						}
					}
				}
			}
		}
		while(r == BUFFERSIZE);

		printf("\n%s", line);

		if(strlen(line) + strlen(word) > 39) printf("\n");

		printf(word);

		cbm_close(2); cbm_close(15);
		free(collection);
		free(path);
		return 0;
	}
	else
	{
		printf("\nCould not open file.");
		cbm_close(2); cbm_close(15);
		free(collection);
		free(path);
		return 1;
	}
}
